package com.szholly.plug.safe.entity.fun;

import java.util.HashMap;

import org.springframework.stereotype.Component;

import com.szholly.utils.session.SessionFactory;
import com.szholly.utils.session.provider.ISessionProvider;
import com.szholly.utils.util.StringUtils;
import com.szholly.data.general.IQueryFilter;
import com.szholly.data.general.ITable;
import com.szholly.data.general.QueryMap;
import com.szholly.data.general.RowBase;
import com.szholly.data.general.interceptor.DbSelectInterceptorClass;
import com.szholly.data.general.interceptor.InterceptorRegister;
import com.szholly.plug.safe.entity.user.UserEntity;

/**
 * 定义数据权限查询拦截器，给指定的用户，添加数据权限
 */
@Component
public class DataFunctionSelectInterceptor extends DbSelectInterceptorClass {

	/**
	 * 注册拦截接口
	 */
	static {
		DataFunctionSelectInterceptor interceptor = new DataFunctionSelectInterceptor();
		InterceptorRegister.RegDbInterceptor(interceptor);
	}
	
	@Override
	public void BeforeDbSelectOption(ITable table, Object queryFilter) {
		ISessionProvider session = SessionFactory.getSession();
		UserEntity userEntity = (UserEntity) session.getObject(ISessionProvider.UserEntity);
		if(userEntity==null){
			return;
		}
		if(userEntity.getIsSuperAdmin()){
			return;
		}
		
		// 数据表名称，权限记录
		@SuppressWarnings("unchecked")
		HashMap<String, String> sqlCache = (HashMap<String, String>) SessionFactory.getSession()
				.getObject(ISessionProvider.SQLList);
		if(sqlCache.containsKey(table.getMetadataTableName())){
			String strWhere = sqlCache.get(table.getMetadataTableName());
			if(!StringUtils.IsNullOrSpace(strWhere)){
				strWhere = replace(strWhere, userEntity);
				if(queryFilter instanceof IQueryFilter){
					IQueryFilter qf = (IQueryFilter)queryFilter;
					if(!StringUtils.IsNullOrSpace(qf.getWhereString())){
						qf.setWhereString("(" + qf.getWhereString() 
								+ ") and (" + strWhere + ")" );
					}else{
						qf.setWhereString(strWhere);
					}
				} else if(queryFilter instanceof QueryMap){
					QueryMap qm = (QueryMap)queryFilter;
					qm.setWhereString(strWhere);
				} else if(queryFilter instanceof RowBase){
					throw new RuntimeException("不支持rowbase条件下的数据权限过滤！");
				}
			}
		}
	}
	
	private String replace(String where, UserEntity userEntity){
		if(StringUtils.IsNullOrSpace(where)){
			return "";
		}
		
		where = where.replace("{userid}", userEntity.getUserID());
		where = where.replace("{username}", userEntity.getUserName());
		where = where.replace("{orgid}", userEntity.getOrgID());
		where = where.replace("{deptid}", userEntity.getDEPTID());
		if(where.indexOf("{nextorgid}") >-1 && !StringUtils.IsNullOrEmpty(userEntity.getOrgID())){
			String nextOrgid=userEntity.getOrgID();//.substring(0,2);
			if(nextOrgid.endsWith("0000") && nextOrgid.length()==6){
				nextOrgid=nextOrgid.substring(0,2);
			}else if(nextOrgid.endsWith("00") && nextOrgid.length()==6){
				nextOrgid=nextOrgid.substring(0,4);
			}else{
				nextOrgid=nextOrgid.substring(0,6);
			}
			where = where.replace("{nextorgid}", nextOrgid);
		}
		return where;
	}

	@Override
	public void DbSelectOptionException(ITable table, Object queryFilter,
			Exception ex) {
		
	}

	@Override
	public void AfterDbSelectOption(ITable table, Object queryFilter,
			Object queryData) {
		
	}

	@Override
	public String getInterceptorName() {
		return null;
	}
}